﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Estimate.Common.Estimate;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Entity;
using Estimate.Common.DataFileFinder;
using Estimate.Reports;

namespace EstimateService
{
    /// <summary>
    /// 集体排名报表数据接口
    /// </summary>
    public class HandlerCollectionRankReport : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string begindate = context.Request.QueryString["begindate"] != null ? context.Request.QueryString["begindate"].ToString() : "";
            string enddate = context.Request.QueryString["enddate"] != null ? context.Request.QueryString["enddate"].ToString() : "";
            string tabletype = context.Request.QueryString["tabletype"] != null ? context.Request.QueryString["tabletype"].ToString() : "";
            string semitype = context.Request.QueryString["semitype"] != null ? context.Request.QueryString["semitype"].ToString() : "";
            string spans = context.Request.QueryString["spans"] != null ? context.Request.QueryString["spans"].ToString() : "";
            string tid = context.Request.QueryString["tid"] != null ? context.Request.QueryString["tid"].ToString() : "";

            //获取回调函数名
            string callback = context.Request.QueryString["callback"];
             //json数据
            string json = GetCollectionRankReportData(begindate, enddate, tabletype, semitype, spans, tid);
  
            context.Response.ContentType = "application/json";
            //输出：回调函数名(json数据)
             context.Response.Write(callback + "(" + json + ")");

            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 集体排名表
        /// </summary>
        /// <param name="begindate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="tabletype"></param>
        /// <param name="semitype"></param>
        /// <param name="spans"></param>
        /// <param name="tid">执行任务号</param>
        /// <returns></returns>
        public string GetCollectionRankReportData(string begindate, string enddate, string tabletype, string semitype, string spans, string tid)
        {
            string connectionString = ConfigurationManager.AppSettings["ConnectionString"];

            DateTime beginDate = DateTime.Parse(begindate);
            DateTime endDate = DateTime.Parse(enddate);
            CollectionRankType tableType = (CollectionRankType)Enum.Parse(typeof(CollectionRankType), tabletype);
            ForeSemiType semiType = (ForeSemiType)Enum.Parse(typeof(ForeSemiType), semitype);

            List<ForeSpanType> spanList = new List<ForeSpanType>();
            foreach (string s in spans.Split(','))
                spanList.Add((ForeSpanType)Enum.Parse(typeof(ForeSpanType), s.Replace("'", "")));
            ForeSpanType[] spanArray = spanList.ToArray();

            ForeSendpointType[] sendPoints = new ForeSendpointType[] { ForeSendpointType.H06, ForeSendpointType.H16 };

            Estimate.Reports.CollectionRankReport collectionRankReport = new Estimate.Reports.CollectionRankReport(connectionString, tableType, tid);
            collectionRankReport.PrepareData(beginDate, endDate,semiType, spanArray, sendPoints);
            return collectionRankReport.GetCityScoreResultString();

        }


    }
}